﻿using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Dto.YS;
using DFGK_WMS.Model.Enum;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Common
{
    public class WcsHttpClient
    {
        private readonly HttpClientHelper httpClientFactory;
        private readonly ILogger<WcsHttpClient> logger;

        public WcsHttpClient(HttpClientHelper httpClientFactory, ILogger<WcsHttpClient> logger)
        {
            this.httpClientFactory = httpClientFactory;
            this.logger = logger;
        }




        public WcsTaskResultDto OutTaskApi(WcsTaskDto m)
        {
            string url = Config.WcsBaseUrl + "/wcs/ExternalApi/SendTask";
            WcsTaskResultDto? wcsTaskResultDto = null;
            try
            {
                wcsTaskResultDto = httpClientFactory.PostAsync<WcsTaskResultDto>(url, JsonConvert.SerializeObject(m), timeOut: 180, responseContentType: "application/json").Result;

                if (wcsTaskResultDto.Code == EmApiResCode.Success)
                {
                    logger.LogInformation("Wcs任务下发成功");
                }
                else
                {
                    logger.LogError("Wcs任务下发失败,msg:" + wcsTaskResultDto.ReturnInfo);
                }
                return wcsTaskResultDto;
            }
            catch (Exception ex)
            {
                logger.LogError("Wcs任务下发异常,msg:" + ex.Message);
                return new WcsTaskResultDto()
                {
                    Code = EmApiResCode.Error,
                    Msg = ex.Message
                };
            }
            finally
            {
                if (m.TaskType == 1)
                {
                    logger.LogInformation($"Wcs入库任务【url:{url}】【入参：{JsonConvert.SerializeObject(m)}】【出参：{JsonConvert.SerializeObject(wcsTaskResultDto)}】");
                }else if (m.TaskType==2)
                {
                    logger.LogInformation($"Wcs出库任务【url:{url}】【入参：{JsonConvert.SerializeObject(m)}】【出参：{JsonConvert.SerializeObject(wcsTaskResultDto)}】");
                }
                else
                {
                    logger.LogInformation($"Wcs移库任务【url:{url}】【入参：{JsonConvert.SerializeObject(m)}】【出参：{JsonConvert.SerializeObject(wcsTaskResultDto)}】");
                }

            }
        }

        public WcsTaskResultDto TranTaskApi(WcsTranTaskDto m)
        {
            string url = Config.WcsBaseUrl + "/wcs/ExternalApi/SendMoveTask";
            WcsTaskResultDto? wcsTaskResultDto = null;
            try
            {
                wcsTaskResultDto = httpClientFactory.PostAsync<WcsTaskResultDto>(url, JsonConvert.SerializeObject(m), timeOut: 180, responseContentType: "application/json").Result;

                if (wcsTaskResultDto.Code == EmApiResCode.Success)
                {
                    logger.LogInformation("Wcs任务下发成功");
                }
                else
                {
                    logger.LogError("Wcs任务下发失败,msg:" + wcsTaskResultDto.ReturnInfo);
                }
                return wcsTaskResultDto;
            }
            catch (Exception ex)
            {
                logger.LogError("Wcs任务下发异常,msg:" + ex.Message);
                return new WcsTaskResultDto()
                {
                    Code = EmApiResCode.Error,
                    Msg = ex.Message
                };
            }
            finally
            {
                logger.LogInformation($"Wcs移库任务【url:{url}】【入参：{JsonConvert.SerializeObject(m)}】【出参：{JsonConvert.SerializeObject(wcsTaskResultDto)}】");
            }
        }

        public WcsTaskResultDto ChangeTask(ChangeTaskDto m)
        {
            string url = Config.WcsBaseUrl + "/wcs/ExternalApi/ChangeTask";
            WcsTaskResultDto? wcsTaskResultDto = null;
            try
            {
                wcsTaskResultDto = httpClientFactory.PostAsync<WcsTaskResultDto>(url, JsonConvert.SerializeObject(m), timeOut: 180, responseContentType: "application/json").Result;

                if (wcsTaskResultDto.Code == EmApiResCode.Success)
                {
                    logger.LogInformation("Wcs任务下发成功");
                }
                else
                {
                    logger.LogError("Wcs任务下发失败,msg:" + wcsTaskResultDto.ReturnInfo);
                }
                return wcsTaskResultDto;
            }
            catch (Exception ex)
            {
                logger.LogError("Wcs任务下发异常,msg:" + ex.Message);
                return new WcsTaskResultDto()
                {
                    Code = EmApiResCode.Error,
                    Msg = ex.Message
                };
            }
            finally
            {
                logger.LogInformation($"Wcs移库任务【url:{url}】【入参：{JsonConvert.SerializeObject(m)}】【出参：{JsonConvert.SerializeObject(wcsTaskResultDto)}】");
            }
        }

        public WcsTaskResultDto StartMachineTaskApi(ReceivingBillOrderDto m)
        {
            string url = Config.WcsBaseUrl + "/wcs/ExternalApi/LpStationStatus";
            WcsTaskResultDto? wcsTaskResultDto = null;
            try
            {
                wcsTaskResultDto = httpClientFactory.PostAsync<WcsTaskResultDto>(url, JsonConvert.SerializeObject(m), timeOut: 180, responseContentType: "application/json").Result;

                if (wcsTaskResultDto.Code == EmApiResCode.Success)
                {
                    logger.LogInformation("Wcs任务下发成功");
                }
                else
                {
                    logger.LogError("Wcs任务下发失败,msg:" + wcsTaskResultDto.ReturnInfo);
                }
                return wcsTaskResultDto;
            }
            catch (Exception ex)
            {
                logger.LogError("Wcs任务下发异常,msg:" + ex.Message);
                return new WcsTaskResultDto()
                {
                    Code = EmApiResCode.Error,
                    Msg = ex.Message
                };
            }
            finally
            {
                logger.LogInformation($"Wcs机械爪入库任务【url:{url}】【入参：{JsonConvert.SerializeObject(m)}】【出参：{JsonConvert.SerializeObject(wcsTaskResultDto)}】");
            }
        }
    }
}
